#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int N = 310;
typedef pair<int, int> PII;
// 思路：遍历两遍，第一遍把所有周边有雷的全部标记，然后用flood fill查看有多少联通块
char board[N][N];
bool flag[N][N];
int n, T;
int num, ans;

int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[8] = {1, -1, 1, -1, -1, 1, 0, 0};

void add_num(int x, int y)
{
    if(board[x][y] == '.')
        board[x][y] = '1', ++num;
    else
        board[x][y]++;
}

void mark()
{
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
        {
            if(board[i][j] == '*')
                for(int k = 0; k < 8; ++k)
                {
                    int x = i + dx[k], y = j + dy[k];
                    if(x > 0 && x <= n && y > 0 && y <= n && board[x][y] != '*')
                        add_num(x, y);
                }
        }
}

void bfs(int x, int y)
{
    queue<PII> q;
    ++ans;
    q.push({x, y});
    flag[x][y] = true;
    while(q.size())
    {
        auto tt = q.front();
        int tx = tt.first, ty = tt.second;
        q.pop();
        for(int i = 0; i < 8; ++i)
        {
            int xx = tx + dx[i], yy = ty + dy[i];
            if(xx > 0 && xx <= n && yy > 0 && yy <= n && board[xx][yy] != '*' && !flag[xx][yy])
            {
                if(board[xx][yy] == '.') q.push({xx, yy});
                else --num;
                flag[xx][yy] = true;
            }
        }
    }
    
}

int main()
{
    cin >> T;
    for(int i = 1; i <= T; ++i)
    {
        memset(flag, 0, sizeof flag);
        num = 0, ans = 0;
        cin >> n;
        for(int i = 1; i <= n; ++i)
            cin >> (board[i] + 1);
        mark();
        // cout << num << endl;
        // num 标记了周围有雷的数量
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                if(board[i][j] == '.' && !flag[i][j])
                    bfs(i, j);
        printf("Case #%d: %d\n", i, ans + num);
    }
    return 0;
}